package com.example.resturant.service.impl;

import com.example.resturant.dto.AddressDTO;
import com.example.resturant.dto.LoginRequestDTO;
import com.example.resturant.dto.RegisterRequestDTO;
import com.example.resturant.exception.ServiceException;
import com.example.resturant.model.Address;
import com.example.resturant.model.User;
import com.example.resturant.repository.AddressRepository;
import com.example.resturant.repository.UserRepository;
import com.example.resturant.service.UserService;
import com.example.resturant.vo.AddressVO;
import com.example.resturant.vo.UserVO;
import lombok.var;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

@Service
public class UserServiceImpl implements UserService {

    @Resource
    private UserRepository userRepository;
    @Resource
    private AddressRepository addressRepository;

    @Override
    public UserVO login(LoginRequestDTO loginRequest) {
        User user = userRepository.findByUsername(loginRequest.getUsername());
        if (user == null) {
            throw new ServiceException("账号不存在，请先注册");
        }
        if (ObjectUtils.notEqual(user.getPassword(), loginRequest.getPassword())) {
            throw new ServiceException("用户名或者密码错误");
        }
        UserVO userVO = new UserVO();
        userVO.setId(user.getId());
        userVO.setUsername(user.getUsername());
        return userVO;
    }

    @Override
    public UserVO register(RegisterRequestDTO registerRequest) {
        if (ObjectUtils.notEqual(registerRequest.getPassword(), registerRequest.getPasswordAgain())) {
            throw new ServiceException("两次密码不一致");
        }
        User user = userRepository.findByUsername(registerRequest.getUsername());
        if (user  != null) {
            throw new ServiceException("用户名已存在");
        }
        user = new User();
        user.setUsername(registerRequest.getUsername());
        user.setPassword(registerRequest.getPassword());
        user = userRepository.save(user);
        UserVO userVO = new UserVO();
        userVO.setId(user.getId());
        userVO.setUsername(user.getUsername());
        return userVO;
    }

    @Override
    public List<AddressVO> listAddresses(Integer userId) {
        List<Address> addresses = addressRepository.findByUserIdAndDeleted(userId, false);
        return addresses.stream().map(address -> {
            AddressVO addressVO = new AddressVO();
            BeanUtils.copyProperties(address, addressVO);
            return addressVO;
        }).collect(Collectors.toList());
    }

    @Override
    public void saveAddress(Integer userId, AddressDTO address) {
        Address addr = new Address();
        BeanUtils.copyProperties(address, addr);
        addr.setUserId(userId);
        addr.setDeleted(false);
        addressRepository.save(addr);
    }

    @Override
    public void delAddress(Integer addressId) {
        Optional<Address> addressOptional = addressRepository.findById(addressId);
        if (!addressOptional.isPresent()) {
            return;
        }
        Address address = addressOptional.get();
        address.setDeleted(true);
        addressRepository.save(address);
    }
}
